package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("sessions")
public class SessionController {
    @Autowired
    private IUserService userService;
    @PostMapping
    public JSONResult login(@RequestParam String email, @RequestParam String password){
        JSONResult json=new JSONResult();
        try {
            User user = userService.login(email, password);
            //把用户返回给移动端
            json.setResult(user);
            //把用户放到session中
            UserContext.setCurrentUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
    @DeleteMapping
    public JSONResult logout(HttpSession session){
       //注销会话
        session.invalidate();
        return new JSONResult();
    }
}
